package com.frejoys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.black.BlackAddDto;
import com.frejoys.common.dto.black.BlackPageDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.enums.EAccountType;
import com.frejoys.common.enums.EBlack;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.util.TimeUtil;
import com.frejoys.common.vo.black.BlackInfoVo;
import com.frejoys.common.vo.black.BlackPageVo;
import com.frejoys.dao.entity.Black;
import com.frejoys.dao.mapper.BlackMapper;
import com.frejoys.service.BlackService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

// 黑名单
@Service
@RequiredArgsConstructor
public class BlackServiceImpl extends ServiceImpl<BlackMapper, Black> implements BlackService {

    @Override
    public boolean addInfo(BlackAddDto dto) {
        if (isExist(dto.getMark(), dto.getType())) {
            return true;
        }

        return save(new Black() {{
            setMark(dto.getMark());
            setContent(dto.getContent());
            setCreateTime(TimeUtil.unixTime());
            setType(dto.getType());
        }});
    }

    @Override
    public boolean isExist(String mark, Integer type) {
        LambdaQueryWrapper<Black> wrapper = new QueryWrapper().lambda();
        wrapper.select(Black::getId).eq(Black::getMark, mark).eq(Black::getType, type);
        Black v = getOnly(wrapper);
        return v != null;
    }

    @Override
    public boolean isPhoneExist(String phone) {
        return isExist(phone, EBlack.phone.getValue());
    }

    @Override
    public boolean isIpExist(String ip) {
        return isExist(ip, EBlack.ip.getValue());
    }

    @Override
    public void verifyAccount(String accountExtend, String account, Integer type) {
        if (type.equals(EAccountType.phone.getValue())) {
            // phone黑名单
            if (isPhoneExist(account)) {
                throw new GeneralException(HttpStatus.phoneNetworkBusy);
            }
        } else {
            throw new GeneralException(HttpStatus.paramValueError);
        }
    }

    @Override
    public void verifyIp(String ipAddr) {
        if (isIpExist(ipAddr)) {
            throw new GeneralException(HttpStatus.ipNetworkBusy);
        }
    }

    /**
     * 后台分页查询黑名单
     *
     * @param dto
     */
    @Override
    public List<BlackPageVo> pageList(BlackPageDto dto) {
        Page<Black> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<Black> wrapper = Wrappers.lambdaQuery();

        wrapper.select(Black::getId,Black::getType,Black::getCreateTime);

        if (dto.getType() != null) {
            wrapper.eq(Black::getType, dto.getType());
        }

        if (StrUtil.isNotEmpty(dto.getStartTime())) {
            wrapper.ge(Black::getCreateTime, TimeUtil.unixTime(dto.getStartTime()));
        }

        if (StrUtil.isNotEmpty(dto.getEndTime())) {
            wrapper.le(Black::getCreateTime, TimeUtil.unixTime(dto.getEndTime()));
        }

        List<Black> blacks = baseMapper.selectList(page, wrapper);

        if (CollectionUtil.isNotEmpty(blacks)) {
            return BeanUtil.copyToList(blacks, BlackPageVo.class);
        }
        return Collections.emptyList();
    }

    /**
     * 获取黑名单详情
     *
     * @param dto
     */
    @Override
    public BlackInfoVo getInfo(IdDto dto) {
        LambdaQueryWrapper<Black> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Black::getId,dto.getId());
        Black only = getOnly(wrapper);
        return BeanUtil.copyProperties(only, BlackInfoVo.class);
    }

}